package com.demo.ui.fragment;

import android.database.Cursor;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;

import com.demo.bbdd.CursorUtils;
import com.demo.bbdd.NTContract;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class PlacesMapFragment extends SupportMapFragment implements
		OnMarkerClickListener, OnMyLocationChangeListener,
		OnInfoWindowClickListener, PlacesInterface, LoaderCallbacks<Cursor> {

	private GoogleMap map;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		map = getMap();
		map.setMyLocationEnabled(true);

		map.setOnMarkerClickListener(this);
		map.setOnMyLocationChangeListener(this);
		map.setOnInfoWindowClickListener(this);

		if (map.getMyLocation() != null) {
			onMyLocationChange(map.getMyLocation());
		}

		getPointsFromDb();

	}

	@Override
	public void getPointsFromDb() {
		getLoaderManager().initLoader(1, null, this);
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		map.moveCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
		map.animateCamera(CameraUpdateFactory.zoomTo(14f));
		return false;
	}

	@Override
	public void onInfoWindowClick(Marker marker) {
		map.moveCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
		map.animateCamera(CameraUpdateFactory.zoomTo(15f));
		map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		marker.hideInfoWindow();
	}

	@Override
	public void onMyLocationChange(Location location) {
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();

			map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)));
			map.animateCamera(CameraUpdateFactory.zoomTo(6f));
		}

	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(getActivity(), NTContract.PLACE.URI, null,
				null, null, NTContract.PLACE.TIME);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
		if (c.moveToFirst()) {
			do {
				cursorToPlacemark(c);
			} while (c.moveToNext());
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		map.clear();
	}

	private void cursorToPlacemark(Cursor c) {
		String title = CursorUtils.getString(c, NTContract.PLACE.NAME);
		double lat = CursorUtils.getDouble(c, NTContract.PLACE.LAT);
		double lng = CursorUtils.getDouble(c, NTContract.PLACE.LNG);

		drawMarker(title, lat, lng);
	}

	private void drawMarker(String title, double lat, double lng) {
		map.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title(
				title));
	}
}
